package com.xja.friend.controller;

import com.github.pagehelper.Page;

import com.xja.friend.entity.Chatuser;
import com.xja.friend.entity.Friend;
import com.xja.friend.entity.Message;
import com.xja.friend.service.ChatuserService;
import com.xja.friend.service.MessageService;
import org.springframework.web.bind.annotation.*;
import com.xja.friend.service.FriendService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.websocket.server.PathParam;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("user")
@SessionAttributes({"loginuser"})
public class ChatuserController {
    Logger logger = LoggerFactory.getLogger(ChatuserController.class);

    @Autowired
    HttpServletRequest request;

    @Resource
    private ChatuserService chatuserService;

    @Autowired
    private FriendService friendService;

    @Resource
    private MessageService messageService;

    @PostMapping("chatuser")
    public Map<String,Object> add(@RequestBody Chatuser chat){
        System.out.println("chatuser:"+chat);
        Map<String,Object> map = new HashMap<>();
        try{
            chatuserService.insert(chat);
            map.put("status","true");
            return map;

        }catch (Exception e){
            e.printStackTrace();
        }
        map.put("status","false");
        return map;
    }
    @GetMapping("login")
    public Map<String,Object> loginuser(String userid,String password,HttpServletRequest request){
        logger.info("登录功能");
        Map<String,Object> resuMap = new HashMap<>();
        try {
            Chatuser loginuser = chatuserService.seleteByName(userid);
            System.out.println(5165+userid);
            if (loginuser==null){
                resuMap.put("msg","用户不存在");
                resuMap.put("status","false");
                return resuMap;
            }
            if (!loginuser.getUserid().equals(userid)){
                resuMap.put("msg","用户名不正确");
                resuMap.put("status","false");
                return resuMap;
            }
            if (!loginuser.getPassword().equals(password)){
                resuMap.put("msg","密码不正确");
                resuMap.put("status","false");
                return resuMap;
            }
            HttpSession session = request.getSession();
            session.setAttribute("loginuser",loginuser);
            resuMap.put("loginuser",loginuser);
            resuMap.put("status","true");
            return resuMap;
        }catch (Exception e){
            logger.info("登录失败");
            e.printStackTrace();
            resuMap.put("msg","有问题");
        }
        return resuMap;
    }
    @GetMapping("list")
    public Map<String,Object> list(Integer npage, String userid, String nickname){
        logger.info("查询好友");
        Map<String,Object> resuMap = new HashMap<>();
        Map<String,Object> map = new HashMap<>();
        try {

            if (userid!=null && userid!=""){
                map.put("userid",userid);
            }
            if (nickname!=null && nickname!=""){
                map.put("nickname",nickname);
            }
            if (npage==null){
                npage=1;
            }

            Page<Chatuser> chatuserPage = chatuserService.seletePage(npage,map);
            resuMap.put("chatuserList",chatuserPage.getResult());
            resuMap.put("chatuserCount",chatuserPage.getTotal());
            resuMap.put("pageCount",chatuserPage.getPages());
            resuMap.put("npage",npage);
            return resuMap;
        }catch (Exception e){
            e.printStackTrace();
        }
        return resuMap;
    }
    @GetMapping("jhy")
    public Map<String,Object> jhy(Integer cid){
        logger.info("添加好友");
        Map<String,Object> resuMap = new HashMap<>();
        try {
            Chatuser loginuser = (Chatuser) request.getSession().getAttribute("loginuser");
            System.out.println("用户"+loginuser.getNickname());
            System.out.println("id"+cid);
            Chatuser chatuser = chatuserService.seleteBycid(cid);
            if (chatuser.getChecktype()==0){
                System.out.println("添加啦");
                Friend friend = new Friend();
                friend.setMineid(loginuser.getId());
                friend.setFriendid(cid);
                friendService.insert(friend);
                resuMap.put("msg","true");
                return resuMap;
            }
            if (chatuser.getChecktype()==2){
                resuMap.put("msg","flase");
                return resuMap;
            }
            resuMap.put("msg","yz");
            return resuMap;
        }catch (Exception e){
            e.printStackTrace();
        }
        return resuMap;
    }



    @GetMapping("chatuser")
    public Map<String,Object> nick(){
        Map<String,Object> map = new HashMap<>();
        try{
            Chatuser loginuser = (Chatuser) request.getSession().getAttribute("loginuser");
            List<Chatuser> chatuserList = chatuserService.findNickName(loginuser.getId());
            map.put("chatuserList",chatuserList);
            map.put("loginuser",loginuser);
            return map;

        }catch (Exception e){
            e.printStackTrace();
        }

        return null;
    }

    @GetMapping("findchatuser")
    public Map<String,Object> findchatuser(@PathParam("toid") Integer toid){
        Map<String,Object> map = new HashMap<>();
        try{
            Chatuser loginuser = (Chatuser) request.getSession().getAttribute("loginuser");
            //点击出现聊天记录 id为toid
            List<Message> messageList =messageService.findHDXinXi(loginuser.getId(),toid);

            Chatuser chatuser = chatuserService.selectByPrimaryKey(toid);
            map.put("messageList",messageList);
            map.put("loginuser",loginuser);
            map.put("chatuser",chatuser);
            return map;
        }catch (Exception e){
            e.printStackTrace();
        }

        return null;
    }

    //获取个人设置信息
    @GetMapping("chatuserdan")
    public Map<String,Object> chatuserdan(){
        Map<String,Object> map = new HashMap<>();
        try{
            Chatuser chatusers = (Chatuser) request.getSession().getAttribute("loginuser");
            Chatuser chatuser = chatuserService.selectByPrimaryKey(chatusers.getId());
            map.put("chatuser",chatuser);
            return map;

        }catch (Exception e){
            e.printStackTrace();
        }

        return null;
    }
    //修改个人设置信息
    @PutMapping("updatechatuserdan")
    public Map<String,Object> updatechatuserdan(@RequestBody Chatuser chatuser){
        Map<String,Object> map = new HashMap<>();
        try{
            Chatuser chatusers = (Chatuser) request.getSession().getAttribute("loginuser");
            chatuser.setId(chatusers.getId());
            chatuserService.updateByPrimaryKey(chatuser);
            map.put("status","true");
            return map;

        }catch (Exception e){
            e.printStackTrace();
        }

        return null;
    }

    //判断密码


}
